package cn.john.org.controller;

import cn.john.basic.exception.BusinessException;
import cn.john.basic.util.AjaxResult;
import cn.john.basic.util.PageList;
import cn.john.org.domain.Shop;
import cn.john.org.query.ShopQuery;
import cn.john.org.service.IShopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/shop")
@Api(tags = "员工接口",description = "员工接口详细描述")
public class ShopController {
    @Autowired
    private IShopService shopService;

    /**
     * 新增和修改 put
     * @param shop
     * @return
     */
    @PutMapping
    @ApiOperation(value = "员工添加或修改",notes = "如果有id就是修改,没有id的话则是添加")
    public AjaxResult addOrUpdate(@RequestBody Shop shop){
        try {
            if(shop.getId() != null){
                shopService.update(shop);
            }else {
                shopService.add(shop);
            }
            return  new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return  AjaxResult.setMessage();
        }
    }

    /**
     * 删除 delete
     * @PathVariable 从URL中获取占位符的值
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "员工删除",notes = "根据传入id删除数据")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            shopService.remove(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.setMessage();
        }
    }

    /**
     * 查询一条 get
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "查询一条数据",notes = "根据传入id查询员工中的一条数据")
    public Shop get(@PathVariable("id") Long id){
        return shopService.queryById(id);
    }

    /**
     * 查询所有
     * @return
     */
    @GetMapping("/list")
    @ApiOperation(value = "查询所有数据",notes = "查询员工所有数据")
    public List<Shop> getAll(){
        return shopService.queryAll();
    }

    /**
     * 分页 + 高级查询 post
     * @param query
     * @return
     */
    @PostMapping
    @ApiOperation(value = "分页+高级查询")
    public PageList<Shop> getPage(@RequestBody ShopQuery query){
        return shopService.queryPage(query);
    }


    /**
     *  如果前端传的是数组，那么后端可以使用list或者array接收都可以
     * @param ids 需要批量删除的id
     * @return
     */
    @PatchMapping
    public AjaxResult patchDelete(@RequestBody List<Long> ids){
        try {
            shopService.patchDelete(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.setMessage();
        }
    }

    /**
     * 店铺注册入驻
     * @param shop
     * @return
     */
    @PostMapping("/settlement")
    public AjaxResult settledIn(@RequestBody Shop shop){
        try {
            shopService.settledIn(shop);
            return new AjaxResult();
        } catch (BusinessException e){
          return new AjaxResult().setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return  AjaxResult.setMessage();
        }
    }

}
